﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Web.Routing;
using Moq;
using System.Web;

namespace KMS.Web.Tests.routes
{
    /// <summary>
    /// Summary description for RouteTests
    /// </summary>
    [TestClass]
    public class RouteTests
    {
        public RouteTests()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void DefaultUrlRoutesToNewsControllerAndListAction()
        {
            RouteCollection routes = new RouteCollection();
            MvcApplication.RegisterRoutes(routes);

            var httpContextMock = new Mock<HttpContextBase>();
            httpContextMock.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/");

            RouteData routeData = routes.GetRouteData(httpContextMock.Object);

            Assert.IsNotNull(routeData, "Should have found the route");
            Assert.AreEqual("News", routeData.Values["controller"]);
            Assert.AreEqual("List", routeData.Values["action"]);
            Assert.AreEqual(String.Empty, routeData.Values["id"]);
        }

        [TestMethod]
        public void ProjectControllerGoToActionCanBeRecognized()
        {
            RouteCollection routes = new RouteCollection();
            MvcApplication.RegisterRoutes(routes);

            var httpContextMock = new Mock<HttpContextBase>();
            httpContextMock.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/Projects/5");

            RouteData routeData = routes.GetRouteData(httpContextMock.Object);

            Assert.IsNotNull(routeData, "Should have found the route");
            Assert.AreEqual("Projects", routeData.Values["controller"]);
            Assert.AreEqual("GoTo", routeData.Values["action"]);
            Assert.AreEqual("5", routeData.Values["projectId"]);
        }

        [TestMethod]
        public void ProjectControllerAddResourceActionCanBeRecognized()
        {
            RouteCollection routes = new RouteCollection();
            MvcApplication.RegisterRoutes(routes);

            var httpContextMock = new Mock<HttpContextBase>();
            httpContextMock.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/Projects/5/AddResource");

            RouteData routeData = routes.GetRouteData(httpContextMock.Object);

            Assert.IsNotNull(routeData, "Should have found the route");
            Assert.AreEqual("Projects", routeData.Values["controller"]);
            Assert.AreEqual("AddResource", routeData.Values["action"]);
            Assert.AreEqual("5", routeData.Values["projectId"]);
        }
    }
}
